package com.pengcheng.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengcheng.common.utils.PageUtils;
import com.pengcheng.common.utils.Query;
import com.pengcheng.modules.sys.dao.SysUserDao;
import com.pengcheng.modules.sys.entity.SysUserEntity;
import com.pengcheng.modules.sys.service.SysUserService;
import com.pengcheng.modules.sys.vo.UserRoleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Autowired
    SysUserDao sysUserDao;

    // 条件查询,分页查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String)params.get("username");

        IPage<SysUserEntity> page = this.page(
                new Query<SysUserEntity>().getPage(params),
                new QueryWrapper<SysUserEntity>()
                        .like(StringUtils.isNotBlank(username),"username", username)
        );

        return new PageUtils(page);
    }

    // 批量删除
    @Override
    public void deleteBatch(Long[] userIds) {
        this.removeByIds(Arrays.asList(userIds));
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
//        return baseMapper.queryRoleIdList(userId);
        return null;
    }

    @Override
    public void update(SysUserEntity user) {
//        if(StringUtils.isBlank(user.getPassword())){
//            user.setPassword(null);
//        }else{
//            user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
//        }
        this.updateById(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
//        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    public List<UserRoleVo> userRoleList() {
        return sysUserDao.selectUserRoleList();
    }
}
